package com.fairytask.ui
{
	import com.fairytask.engine.GameStage;
	import com.fairytask.engine.components.text.Label;
	import com.fairytask.engine.components.window.Window;
	import com.fairytask.event.ChatEvent;
	import com.fairytask.managers.ViewManage;
	import com.fairytask.ui.base.FrameUI;
	import com.greensock.TweenLite;
	import com.greensock.TweenNano;
	import com.greensock.easing.Cubic;
	
	import flash.events.TimerEvent;
	import flash.filters.GlowFilter;
	import flash.text.TextFormat;
	import flash.utils.Timer;
	
	/**
	 *系统提示框，由下向上浮动 
	 * @author pangj
	 * 
	 */
	public class SysHintUI extends FrameUI
	{
		private static var _self :SysHintUI=null;
		private var startY   :int  = 320;           //起始Y坐标
		private var endY     :int  = 150;           //终点Y坐标
		private var format	  :TextFormat   = null;  //字体格式
		private var strArr   :Array=[];             //需要显示的字符串 数组       
		private var strLabel :Array=[];             //浮动的字符串标签 数组
		private var strWin   :Array=[];             //浮动的字符串窗体 数组
		private var num      :int = 3;              //最多显示窗体数
		private var delay    :Number = 800;         //相邻窗体显示间隔时间 毫秒
		private var isPlay   :Boolean=false;       //当前是否正在播放
		private var timer    :Timer;                //计时器  每隔delay时间播放一次
		private var index    :int=0;                //索引值  当前播放第index个窗体        
		
	
		public static function getInstance():SysHintUI
		{
			if(null == _self)
			{
				_self = new SysHintUI();
			}
			return _self;
		}
		public function SysHintUI(popUI:Boolean=true)
		{
			super(popUI);
			initEvent();
			initFormat();
			windowBG.mouseEnabled = false;
		}
		/**
		 *监听系统信息 
		 * 
		 */
		private function initEvent():void
		{
			model.addEventListener(ChatEvent.ALERT_MESSAGE,getStr);
		}
		/**
		 *设置字体格式及显示窗体 
		 * 
		 */
		private function initFormat():void
		{
			format = new TextFormat();
			format.font = "宋体";
			format.size = 30;
			for(var i:int=0;i<num;i++)
			{
				//显示标签
				var label:Label = new Label();
				label.tf = format;
				label.mouseEnabled = false;
				label.filters = [new GlowFilter(0x020202, 1, 2, 2, 10, 1)];
				strLabel[i] = label;
				//显示窗体
				var win :Window = new Window(null,windowBG.width,65);
				win.x = windowBG.x;
				win.y = startY;
				win.visible = false;
				win.mouseEnabled = false;
				strWin[i] = win;
				Window(strWin[i]).addChild(strLabel[i]);
				GameStage.uiSpace.addChild(Window(strWin[i]));
			}
		}
		/**
		 *显示前设置 
		 * @param space
		 * @param onshow
		 * 
		 */
		override public function show(space:String=TOPLAYER, onshow:Function=null):void
		{
			super.show(space,onshow);
			ViewManage.maskEnable	=	false;
		}
		/**
		 *获取字符串 
		 * @param evt
		 * 
		 */
		private function getStr(evt:ChatEvent):void
		{
			setShowStr(evt.clientMsg);
		}
		/**
		 *将获取的字符串放入显示数组中 
		 * @param val
		 * 
		 */
		public function setShowStr(val:String):void
		{
			strArr.push(val);
			if(!isPlay)//若当前没有播放，则开始计时播放
			{
				timeStart();
			}
		}
		/**
		 *创建计时器并开始计时 
		 * 
		 */
		private function timeStart():void
		{
			if(null == timer)
			{
				timer = new Timer(delay);
				timer.addEventListener(TimerEvent.TIMER,showStr);
			}
			timer.start();
			show();
			isPlay = true;
		}
		
		/**
		 *显示前设置字符串
		 * 
		 */
		private function showStr(evt:TimerEvent):void
		{
			var showStr:String = strArr.shift();//需要显示的文字
			if(null == showStr || "" == showStr)
				return;
			var i:int = (index++)%num;
			//设置显示文字
			strLabel[i].htmlStrByBorder = showStr;
			//设置提示文字居中显示
			strLabel[i].x = (windowBG.width - strLabel[i].width)/2;
			onPlay(strWin[i]);
		}
		/**
		 * 播放
		 * @param win
		 * 
		 */
		private function onPlay(win:Window):void
		{
			win.visible =true;
			TweenLite.to(win,(num*delay)/1000-0.5, {x:0, y:endY, ease:Cubic.easeOut,onComplete:fadeOut,onCompleteParams:[win]});
		}
		
		/**
		 *播放后淡出 
		 * @param win
		 * 
		 */
		private function fadeOut(win:Window):void
		{
			TweenNano.to(win,0.5,{alpha:0,onComplete:onClear,onCompleteParams:[win]});
		}
		/**
		 *淡出后处理所播放的窗口 
		 * @param win
		 * 
		 */
		private function onClear(win:Window):void
		{
			if(win)
			{
				win.alpha = 1;
				win.visible = false;
				win.y = startY;
			}
			if(strArr.length == 0)
			{
				isPlay = false;
				timer.stop();
				index = 0;
			}
		}
		override public function hide(e:*=null):void
		{
			if(timer)
			{
				timer.stop();
				isPlay = false;
				index = 0;
				strArr = [];
				super.hide(e);
			}
		}
	}
}